package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.projectItem.ProjectItemData;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.GlideCircleTransform;

import java.util.List;


/**
 * Created by Administrator on 2016/11/2.
 */

public class CategoryAdapter extends BaseAdapter {

    private LayoutInflater mLayoutInflater;
    private List<ProjectItemData> dataList;
    private Context context;

    public CategoryAdapter(Context context, List<ProjectItemData> dataList) {
        this.context = context;
        this.dataList = dataList;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        try{
            if(convertView==null){
                holder = new ViewHolder();
                convertView = mLayoutInflater.inflate(R.layout.item_category, parent, false);
                holder.ivCategory = (ImageView) convertView.findViewById(R.id.iv_category);
                holder.tvCategory = (TextView) convertView.findViewById(R.id.tv_category);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            if(null!=dataList.get(position).getThumbnail()&&!"".equals(dataList.get(position).getThumbnail())){
                Glide.with(context).load(Url.IMG_URL+dataList.get(position).getThumbnail()).placeholder(R.mipmap.a1).transform(new GlideCircleTransform(context)).into(holder.ivCategory);
            }
            holder.tvCategory.setText(dataList.get(position).getName());
        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }

    class ViewHolder{
        private ImageView ivCategory;
        private TextView tvCategory;
    }
}
